<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Authenticated shared-key decryption</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.sodium-crypto-secretbox-keygen.html">« sodium_crypto_secretbox_keygen</a></li>
      <li style="float: right;"><a href="function.sodium-crypto-secretbox.html">sodium_crypto_secretbox »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.sodium.html">Sodium 函数</a></li>
    <li>Authenticated shared-key decryption</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.sodium-crypto-secretbox-open" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sodium_crypto_secretbox_open</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.2.0, PHP 8)</p><p class="refpurpose"><span class="refname">sodium_crypto_secretbox_open</span> &mdash; <span class="dc-title">Authenticated shared-key decryption</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.sodium-crypto-secretbox-open-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sodium_crypto_secretbox_open</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$ciphertext</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$nonce</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$key</code></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   Decrypt an encrypted message with a symmetric (shared) key.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.sodium-crypto-secretbox-open-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">ciphertext</code></dt>

    <dd>

     <p class="para">
      Must be in the format provided by <span class="function"><a href="function.sodium-crypto-secretbox.html" class="function">sodium_crypto_secretbox()</a></span>
      (ciphertext and tag, concatenated).
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">nonce</code></dt>

    <dd>

     <p class="para">
      A number that must be only used once, per message. 24 bytes long.
      This is a large enough bound to generate randomly (i.e. <span class="function"><a href="function.random-bytes.html" class="function">random_bytes()</a></span>).
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">key</code></dt>

    <dd>

     <p class="para">
      Encryption key (256-bit).
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sodium-crypto-secretbox-open-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The decrypted string on success 或者在失败时返回 <strong><code>false</code></strong>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.sodium-crypto-secretbox-open-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">nonce</code> has a length of bytes different than
     <a href="sodium.constants.html#constant.sodium-crypto-secretbox-noncebytes" class="link"><strong><code>SODIUM_CRYPTO_SECRETBOX_NONCEBYTES</code></strong></a>
     (24 bytes), a <span class="classname"><a href="class.sodiumexception.html" class="classname">SodiumException</a></span> will be thrown.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">key</code> has a length of bytes different than
     <a href="sodium.constants.html#constant.sodium-crypto-secretbox-keybytes" class="link"><strong><code>SODIUM_CRYPTO_SECRETBOX_KEYBYTES</code></strong></a>
     (32 bytes), a <span class="classname"><a href="class.sodiumexception.html" class="classname">SodiumException</a></span> will be thrown.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 examples" id="refsect1-function.sodium-crypto-secretbox-open-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-994">
    <p><strong>示例 #1 <span class="function"><strong>sodium_crypto_secretbox_open()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;$key&nbsp;must&nbsp;be&nbsp;kept&nbsp;confidential<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">random_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">SODIUM_CRYPTO_SECRETBOX_KEYBYTES</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;Do&nbsp;not&nbsp;reuse&nbsp;$nonce&nbsp;with&nbsp;the&nbsp;same&nbsp;key<br /></span><span style="color: #0000BB">$nonce&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">random_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">SODIUM_CRYPTO_SECRETBOX_NONCEBYTES</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$ciphertext&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sodium_crypto_secretbox</span><span style="color: #007700">(</span><span style="color: #DD0000">'message&nbsp;to&nbsp;be&nbsp;encrypted'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$nonce</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;same&nbsp;nouce&nbsp;and&nbsp;key&nbsp;are&nbsp;required&nbsp;to&nbsp;decrypt&nbsp;the&nbsp;$ciphertext<br /></span><span style="color: #0000BB">$plaintext&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sodium_crypto_secretbox_open</span><span style="color: #007700">(</span><span style="color: #0000BB">$ciphertext</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$nonce</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$plaintext&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$plaintext&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
message to be encrypted
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.sodium-crypto-secretbox-open-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.sodium-crypto-secretbox.html" class="function" rel="rdfs-seeAlso">sodium_crypto_secretbox()</a> - Authenticated shared-key encryption</span></li>
    <li class="member"><span class="function"><a href="function.sodium-crypto-secretbox-keygen.html" class="function" rel="rdfs-seeAlso">sodium_crypto_secretbox_keygen()</a> - Generate random key for sodium_crypto_secretbox</span></li>
    <li class="member"><span class="function"><a href="function.random-bytes.html" class="function" rel="rdfs-seeAlso">random_bytes()</a> - Get cryptographically secure random bytes</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>